<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_data_perangkat extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_data_perangkat/sample_master_model', 'DataPerangkat');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Data Penggunaan Perangkat";
		viewByTemplate('pro_data_perangkat/index', $data, 'grid');
	}

	function id_cabang(){
		$nama_cabang = $this->input->post('option');
		$query = $this->db->query("SELECT id_cabang FROM pro_setup_cabang WHERE nama_cabang='".$nama_cabang."'");
		$hasil = $query->row_array();
		$data['id_cabang']=$hasil['id_cabang'];
		echo json_encode($data);
	}

	function nama_cabang(){
		//$nama_cabang = $this->input->post('option');
		$query = $this->db->query("	SELECT	nama_cabang
									FROM	ADMIN_PRIMKOKAS.PRO_SETUP_CABANG
									ORDER BY id_cabang ASC");
		$hasil = $query->result_array();

		echo json_encode($hasil);
	}

	function create()
	{
		// Display Form Create
		$data['title'] = "Data Penggunaan Perangkat";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";
		viewByTemplate('pro_data_perangkat/form', $data, 'form');
	}

	function AjaxId(){

		$query = $this->db->query("SELECT kode_perangkat FROM (SELECT * FROM pro_barang ORDER BY kode_perangkat DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_perangkat'];
		else
			$MaxID = "P0000000";

		$NextID = (int) substr($MaxID,1,7);

		$NextID++;

		$data['kode_perangkat'] = "P" . sprintf("%07s",$NextID);

		$query2 = $this->db->query("SELECT id_perangkat FROM (SELECT * FROM pro_barang ORDER BY id_perangkat DESC) WHERE rownum <= 1");
				 				if ($query2->row_array()) {
				 					$getquery2 = $query2->row_array();
				 					$rowcount = $getquery2['id_perangkat'];
				 					$autonum = $rowcount + 1;
				 				} else {
								$autonum = 1;
							}
		$data['id_perangkat'] = $autonum;

		echo json_encode($data);
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Penggunaan Perangkat";

		$data['data'] = $this->DataPerangkat->GetSingleView(get('id_perangkat'));
		$id_perangkat = get('id_perangkat');
		$query2 = $this->db->query("SELECT jumlah,jumlah_pakai FROM pro_barang WHERE id_perangkat='".$id_perangkat."'");
		$getquery2 = $query2->row_array();

		$data['jum_pakai'] = $getquery2['jumlah_pakai'];
		$data['jum_awal'] = $getquery2['jumlah'];
		$data['id'] = $id_perangkat;
		//var_dump($data['data']);
		// Define Combo Data
		$data['id_cabang'] = get('id_cabang');
		$data['nama_cabang'] = get('nama_cabang');
		$data['mode'] = "edit";
		viewByTemplate('pro_data_perangkat/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Master Perangkat";
		$data['data'] = $this->DataPerangkat->GetSingleView(get('kode_barang'));
		
		$data['mode'] = "view";
		viewByTemplate('pro_data_perangkat/form', $data, 'form');
	}

	function show()
	{
		// Display Form View
		$data['title'] = "View Data Master Perangkat";
		$data['data'] = $this->DataPerangkat->GetSingleView(get('kode_barang'));
		
		$data['mode'] = "show";
		viewByTemplate('pro_data_perangkat/form', $data, 'form');
	}

	function getdata()
	{
		$this->secure_session();
		echo $this->DataPerangkat->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("id_perangkat") == '') {
			$error->Add("id_perangkat", "Kode Perangkat", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_barang WHERE id_perangkat= '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("id_perangkat"))))."'")) > 0) {
			$error->Add("id_perangkat", "Kode Perangkat", $tpl_duplicate);
		}


		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{

			 //    $tgl_old1 = str_replace(" ", "", $this->input->post('awal_kerja'));

				// $tahun_new1 = substr($tgl_old1, 6, 4);
			 //    $bulan_new1 = substr($tgl_old1, 3, 2);
			 //    $hari_new1   = substr($tgl_old1, 0, 2);

			 //    $tgl_new1 = $tahun_new1."-".$bulan_new1."-".$hari_new1;

			 //    $tgl_old2 = str_replace(" ", "", $this->input->post('akhir_kerja'));

				// $tahun_new2 = substr($tgl_old2, 6, 4);
			 //    $bulan_new2 = substr($tgl_old2, 3, 2);
			 //    $hari_new2   = substr($tgl_old2, 0, 2);

			 //    $tgl_new2 = $tahun_new2."-".$bulan_new2."-".$hari_new2;


				// Fill Post Data
				$data = array(
							'kode_perangkat' => $this->input->post('kode_perangkat'),
							'kode_barang' => $this->input->post('kode_barang'),
							'id_perangkat' => $this->input->post('id_perangkat'),
							'id_barang' => $this->input->post('id_barang'),
							'nama_barang' => $this->input->post('nama_barang'),
							'nama_merek' => $this->input->post('nama_merek'),
							'tipe' => $this->input->post('tipe'),
							'harga' => $this->input->post('harga'),
							'spesifikasi' => $this->input->post('spesifikasi'),
							'kondisi' => $this->input->post('kondisi'),
							//'jumlah' => $this->input->post('jumlah'),
							'harga' => $this->input->post('harga'),
							'jumlah' => $this->input->post('update_stock'),
							'kondisi' => $this->input->post('kondisi'),
							'keterangan' => $this->input->post('keterangan'),
							'jumlah_pakai' => $this->input->post('jumlah_pakai'),
							'jenis_kebutuhan' => $this->input->post('jenis_kebutuhan'),
							'id_cabang' => $this->input->post('id_cabang'),
							'nama_cabang' => $this->input->post('nama_cabang')
							);

				if (post("mode") == "create")
				{
					$this->DataPerangkat->Add($data);
					
					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->DataPerangkat->Update($data, array('id_perangkat'	=> $data['id_perangkat']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$id_perangkat	= get('id_perangkat');
		$pesan = "Data Terhapus";
		if ($id_perangkat != "")
		{
			// Delete Single
			$arr_rule	= array('id_perangkat' => $id_perangkat);
			$ls_result = $this->DataPerangkat->DeleteSingle($arr_rule);

		}
		else
		{
		}
		
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_perangkat' => $row['id_perangkat']);
				$ls_result .= $this->DataPerangkat->DeleteSingle($arr_rule);
			}
		echo $ls_result;
		echo $pesan;
	}
}
?>